package com.rzico.sbl.ui.member

import android.text.TextUtils
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.CommonData
import com.rzico.sbl.viewmodel.BucketViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.glideExt.loadRoundedImage
import com.xinnuo.common_ui.utils.bindLifeCycle
import com.xinnuo.common_ui.utils.subscribeByFinally
import com.xinnuo.slimadapter.SlimAdapter
import net.mikaelzero.mojito.ext.mojito
import kotlin.text.orEmpty

class MemberBucketRecordActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    override fun getViewModel() = get<BucketViewModel>()

    override fun initData() {
        initTitle(title = "空桶记录")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.listLayout.apply {
            emptyLayout.emptyHint.text = "暂无相关记录信息！"

            recycleList.also {
                it.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData(0) },
                    onLoad = {
                        if (!isLoadingMore) {
                            isLoadingMore = true
                            getData(pageNum)
                        }
                    }
                )

                it.addItemDecoration(SpaceTBDecoration())

                mAdapter = SlimAdapter.creator()
                    .register<CommonData>(R.layout.item_bucket_record) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_record_name, bean.memo)
                            text(R.id.item_record_time, bean.createDate.getTimeString())
                            text(R.id.item_record_song, bean.quantity)
                            text(R.id.item_record_hui, bean.returnQuantity)
                            text(R.id.item_record_ya, bean.pledgeQuantity)
                            text(R.id.item_record_tui, bean.refundsQuantity)
                            text(R.id.item_record_jie, bean.quantity)
                            text(R.id.item_record_huan, bean.returnQuantity)
                            text(R.id.item_record_total1, bean.mortgage)
                            text(R.id.item_record_total2, bean.borrow)
                            text(R.id.item_record_hang, bean.hangTotal)

                            visibility(
                                R.id.item_record_layout1,
                                visibleValue { "工单" in bean.memo.orEmpty() }
                            )
                            visibility(
                                R.id.barrelimg,
                                visibleValue { TextUtils.isEmpty(bean.photo) }
                            )
                            with<ImageView>(R.id.barrelimg) {
                                bean.photo
                                it.isGone = TextUtils.isEmpty(bean.photo)
                                if (!TextUtils.isEmpty(bean.photo)){
                                    it.loadRoundedImage(bean.photo)
                                }
                                it.oneClick {
                                    it.mojito(bean.photo.toString())
                                }
                            }

                            visibility(
                                R.id.item_record_layout2,
                                visibleValue {
                                    "押桶" in bean.memo.orEmpty()
                                            || "退桶" in bean.memo.orEmpty()
                                }
                            )

                            visibility(
                                R.id.item_record_layout3,
                                visibleValue {
                                    "借桶" in bean.memo.orEmpty()
                                            || "还桶" in bean.memo.orEmpty()
                                }
                            )
                        }
                    }
                    .attachTo(it)
            }
        }
    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .bucketRcd(
                page = index,
                cardId = intent.getExtra("cardId"),
                barrelId = intent.getExtra("barrelId")
            )
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it)
                        it.notEmpty { pageNum++ }
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

}